package com.example.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    
    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbAgreement;
    private Button btnLogin;
    private TextView tvRegister;
    private TextView tvShowHidePwd;
    private AlertDialog progressDialog; // 替代弃用的ProgressDialog

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_main); // 确保布局文件名正确

        // 初始化控件（避免ID找不到错误）
        initViews();

        // 登录按钮点击事件
        btnLogin.setOnClickListener(v -> login());

        // 注册按钮点击事件
        tvRegister.setOnClickListener(v -> {
            Toast.makeText(MainActivity.this, "跳转到注册页", Toast.LENGTH_SHORT).show();
        });

        // 密码显隐切换
        tvShowHidePwd.setOnClickListener(v -> togglePasswordVisibility());
    }

    private void initViews() {
        // 确保所有ID在activity_main.xml中存在
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbAgreement = findViewById(R.id.cb_agreement);
        btnLogin = findViewById(R.id.btn_login);
        tvRegister = findViewById(R.id.tv_register);
        tvShowHidePwd = findViewById(R.id.tv_show_hide_pwd); // 从XML中获取，避免动态创建错误

        // 初始化进度对话框（避免弃用错误）
        progressDialog = new AlertDialog.Builder(this)
                .setView(R.layout.dialog_progress) // 自定义进度布局
                .setCancelable(false)
                .create();
    }

    private void login() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        // 输入校验
        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!cbAgreement.isChecked()) {
            Toast.makeText(this, "请同意用户协议", Toast.LENGTH_SHORT).show();
            return;
        }

        // 模拟登录
        progressDialog.show();
        new Thread(() -> {
            try {
                Thread.sleep(2000); // 模拟网络请求
                runOnUiThread(() -> {
                    progressDialog.dismiss();
                    // 登录成功跳转首页
                    Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                    intent.putExtra("username", username);
                    startActivity(intent);
                    // 不调用finish()，保留在任务栈中，以便支持singleTask模式测试
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private void togglePasswordVisibility() {
        // 密码显隐逻辑（避免输入类型错误）
        int inputType = etPassword.getInputType();
        if ((inputType & android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0) {
            etPassword.setInputType(
                    android.text.InputType.TYPE_CLASS_TEXT |
                            android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            );
            tvShowHidePwd.setText("隐藏密码");
        } else {
            etPassword.setInputType(
                    android.text.InputType.TYPE_CLASS_TEXT |
                            android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
            );
            tvShowHidePwd.setText("显示密码");
        }
        etPassword.setSelection(etPassword.getText().length()); // 保持光标位置
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}
